<?php
namespace Modules\Node\Entity;

use Core\Mvc\Entity\Entity as BaseEntity;

class Node extends BaseEntity
{
    protected $_entity = 'node';
    protected $_module = 'node';
    protected $_relationships = [
        'user' => [
            'type' => 'hasOne',
            'key' => 'uid',
            'modelKey' => '_id',
            'obid' => true,
            'model' => '\Modules\User\Entity\User'
        ]
    ];
    public $browse=0;
    public $love =0;
    public $commentNum = 0;
    public $socialize = 0;

    public function beforeSave(){
        $this->browse = intval($this->browse);
    }
    public function getLinks()
    {
        if (!$this->_links) {
            $this->_links = [
                'edit' => [
                    'href' => [
                        'for' => 'adminEntityEdit',
                        'entity' => 'node',
                        'id' => $this->getId(),
                    ],
                    'data-target' => 'main',
                    'icon' => 'info',
                    'name' => '编辑',
                ],
                'delete' => [
                    'href' => [
                        'for' => 'adminEntityDelete',
                        'entity' => 'node',
                        'id' => $this->getId(),
                    ],
                    'data-target' => 'main',
                    'icon' => 'danger',
                    'name' => '删除',
                ],
            ];
        }
        return parent::getLinks();
    }

    public function socialize($name){
        if($name != 'browse' && $name != 'love' && $name != 'commentMum'){
            return false;
        }
        $this->{$name} = intval($this->{$name}) + 1;
        $socialize = (0.2*$this->browse)+(0.5*$this->love)+$this->commentNum;
        $this->socialize = intval($socialize);
        return $this->save();
    }
}